package com.yu.dao;

import java.util.List;

import com.yu.pojo.Tuser;

import com.yu.dao.*;

public class TuserDao {
	public List getAll() {
        String sql = "select * from tuser";
        List l = DH.getall(sql, new Tuser(), new String[]{});
        return l;
	}
	
    public Tuser login(String uname, String upwd) {
        String sql = "select * from tuser where uname = ? and upwd = ?";
        List l = DH.getall(sql, new Tuser(), new String[]{uname, upwd});
        System.out.println(l.size());
        if(l.size()!=0) {
            return (Tuser) l.get(0);
        }else {
            return null;
        }
    }
    public Tuser login(String uphone) {
        String sql = "select * from tuser where uphone = ?";
        List l = DH.getall(sql, new Tuser(), new String[]{uphone});
        if (l.size() != 0) {
            return (Tuser) l.get(0);
        } else {
            return null;
        }
    }
    public int save(String uname, String upwd, String realname) {
        String sql = "select * from tuser where uname = ?";
        List l = DH.getall(sql, new Tuser(), new String[]{uname});
        if(l.size()>0) {
        	return -1;
        }else {
        	String sql1 = "select * from tuser ORDER BY id DESC LIMIT 1";
        	List<?> l1 = DH.getall(sql1, new Tuser(), new String[] {});
        	Tuser u = (Tuser)l1.get(0);
        	String id1 = u.getId();
        	int num = Integer.parseInt(id1)+1;
        	String id2 = "";
        	if(num/10000>0) {
        		id2=num+"";
        	}else if(num/1000>0) {
        		id2="0"+num;
        	}else if(num/100>0) {
        		id2="00"+num;
        	}else if(num/10>0) {
        		id2="000"+num;
        	}else {
        		id2="0000"+num;
        	}
        	String sql2 = "insert into tuser(id, uname, upwd,realname) values(?, ?, ?, ?)";
        	int num1 = DH.update(sql2, new String[] {id2, uname, upwd,realname});
        	return num1;
        }

    }
}
